package com.shopping.dao;

import com.shopping.pojo.Store;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Mapper
public interface StoreDao {

    String TABLE_NAME = "store";
    String INSERT_FIELDS = "store_name, store_avatar, level, fans";
    String SELECT_FIELDS = "id, store_name storeName, store_avatar storeAvatar, level, fans";

    @Insert({"insert into ", TABLE_NAME, "(", INSERT_FIELDS, ") values(#{storeName}, #{storeAvatar}, #{level}, #{fans})"})
    int insert(Store store);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where id=#{id}"})
    Store selectById(@Param("id") int id);

    @Select({"select last_insert_id()"})
    Integer lastInsertId();

    @Update({"update ", TABLE_NAME, " set store_name=#{storeName} where id=#{id}"})
    int updateName(@Param("id") int id, @Param("storeName") String storeName);

    @Update({"update ", TABLE_NAME, " set avatar=#{avatar} where id=#{id}"})
    int updateAvatar(@Param("id") int id, @Param("avatar") byte[] avatar);

    @Update({"update ", TABLE_NAME, " set level=level+1 where id=#{id}"})
    int updateLevel(@Param("id") int id);

    @Update({"update ", TABLE_NAME, " set fans=fans+1 where id=#{id}"})
    int updateFans(@Param("id") int id);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where store_name=#{storeName}"})
    Store selectOneByName(@Param("storeName") String storeName);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where store_name like '%${storeName}%'"})
    List<Store> selectByName(@Param("storeName") String storeName);

    @Select({"select store_name from ", TABLE_NAME, " where id=#{storeId}"})
    String selectStoreNameById(@Param("storeId") int storeId);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME})
    List<Store> selectAll();
}
